import { HTTP_REQUEST_URL } from '@/config/app.js';
import store from '@/store/index.js';

// 上传文件
export function postUploadNow(input) {
    const filePath = typeof input === 'string' ? input : (input && (input.path || input.tempFilePath))
    // 兜底：若无法解析路径，直接拒绝
    if (!filePath) return Promise.reject(new Error('Invalid file path'))
    // 获取文件后缀名，缺省则使用jpg
    const lastDot = filePath.lastIndexOf('.')
    const fileExtension = lastDot > -1 ? filePath.slice(lastDot + 1) : 'jpg'
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: `${HTTP_REQUEST_URL}/uploadnow`,
            filePath,
            name: 'file',
            header: {
                'Authorization': store.getters.token
            },
            formData: {
                'filename': `image_${Date.now()}.${fileExtension}`,
            },
            success: (uploadRes) => resolve(uploadRes),
            fail: (error) => reject(error)
        })
    })
}